package com.shizhanzhe.gxb.my.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.MainActivity;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.WXLoginActivity;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.my.bean.Sysset;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.AdPreference;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.gxb.utils.SharedPreferencesUtils;
import com.shizhanzhe.gxb.utils.Util;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YJ
 * @date 2019/1/4
 */
public class SplashActivity extends Activity {
    @BindView(R.id.tv_dao)
    TextView tvDao;
    @BindView(R.id.ll_tiaoguo)
    LinearLayout llTiaoguo;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    private String imgUrl = "";
    private int mtime = 5;
    boolean isRun = true;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mtime--;
            tvDao.setText(mtime + "");
            if (mtime == 0 && isRun) {
                if ((int) SharedPreferencesUtils.getParam(SplashActivity.this, "uid", 0) == 0) {
                    startActivity(new Intent(SplashActivity.this, WXLoginActivity.class));
                } else {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                }
                isRun = false;
                finish();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .transparentBar().statusBarDarkFont(true, 0.2f)
                .init();
        UserBean.uid = (int) SharedPreferencesUtils.getParam(this, "uid", 0);
        initData();
    }

    private void initData() {
        imgUrl = AdPreference.getInstance().getStartupAdPage().getRelatedUrl();
        isFirst();
        if ("".equals(imgUrl)) {
            llTiaoguo.setVisibility(View.GONE);
            mtime = 3;
        } else {
            mtime = 5;
            llTiaoguo.setVisibility(View.VISIBLE);
            llTiaoguo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ((int) SharedPreferencesUtils.getParam(SplashActivity.this, "uid", 0) == 0) {
                        startActivity(new Intent(SplashActivity.this, WXLoginActivity.class));
                    } else {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    }
                    isRun = false;
                    finish();

                }
            });
        }
        sysset();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRun) {
                    try {
                        Thread.sleep(1000); // sleep 1000ms
                        Message message = Message.obtain();
                        message.what = 1;
                        mHandler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
        ivImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (imgUrl != "") {
                    Uri uri = Uri.parse(imgUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            }
        });
    }

    private void sysset() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.sysset, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Sysset sysset = new Gson().fromJson(json, Sysset.class);
                        SharedPreferencesUtils.setParam(SplashActivity.this, "income", sysset.getData().getIncome() + "");
                        SharedPreferencesUtils.setParam(SplashActivity.this, "profit", sysset.getData().getProfit() + "");
                    } catch (Exception e) {
                        Log.d("______exception", json);
                    }
                } else {
                    Toast.makeText(SplashActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void isFirst() {
        String AD_PATH = "";
        AD_PATH = Environment.getExternalStorageDirectory() + "/MFAd/" + Util.getImgName(AdPreference.getInstance().getStartupAdPage().getPicUrl());
        File file = new File(AD_PATH);
        Bitmap bm = BitmapFactory.decodeFile(AD_PATH);
        if (file.exists() && bm != null) {
            ivImg.setScaleType(ImageView.ScaleType.FIT_XY);
            ivImg.setImageBitmap(bm);
        } else {
            ivImg.setScaleType(ImageView.ScaleType.FIT_CENTER);
            Util.deleteFile(AD_PATH);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
